package com.system.server.impl;

import com.system.dao.StudentDao;
import com.system.pojo.Student;
import com.system.pojo.StudentVO;
import com.system.server.StudentService;
import exception.AddStudentError;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Author: E-mail:
 * @Version: 创建时间：2019年04月16日16:42
 * @Description: <描述>
 */
@Service
@Transactional(propagation = Propagation.REQUIRED,rollbackFor = Exception.class)
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;

    @Override
    @Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
    public List<Student> findAll() {
        return studentDao.selectAll();
    }

    @Override
    public List<Student> findStudentByName(String findByName) {
        return studentDao.selectStudentByName(findByName);
    }

    @Override
    public void addStudent(StudentVO student) throws AddStudentError {
        try {
            studentDao.insertStudent(student);
        } catch (Exception e) {
            throw new AddStudentError("添加失败");
        }
    }

    @Override
    public Boolean checkStudentID(Integer userID) {
        StudentVO student = studentDao.findStudentByID(userID);
        if(student==null) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public Student findStudentByID(Integer id) {
        Student student = studentDao.selectStudentById(id);
        return student;
    }

    @Override
    public void modifyStudent(Student student) {
        studentDao.update(student);
    }

    @Override
    public void removeStudent(Integer id) {
        studentDao.deleteStudent(id);
    }
}
